package student;
import student.dto.BookDto;
import student.dto.StudentDto;
import student.dto.TeacherDto;
import student.service.BookService;
import student.service.StudentService;
import student.service.TeacherService;
import student.utils.FileUtil;
import student.utils.Utils;

import java.util.List;
import java.util.Scanner;

/**
 *
 * 主类
 * @author
 * @date 2020/5/9.
 */
public class Main {
    /**
     * 对应的文件路径
     */
    public final static String studentFilePath = "D:/image/student.doc";
    public final static String teacherFilePath = "D:/image/teacher.doc";
    public final static String bookFilePath = "D:/image/book.doc";


    public static List<StudentDto> studentDtos;
    public static List<TeacherDto> teacherDtos;
    public static List<BookDto> bookDtos;
    public static void main(String[] args) {
        new Main().start();
    }

    public void start() {
        initWord();
        showPage();
        Scanner sc = new Scanner(System.in);
        int key = sc.nextInt();
        FileUtil fileUtil = new FileUtil();
        StudentService studentService = new StudentService();
        BookService bookService = new BookService();
        TeacherService teacherService = new TeacherService();
        switch (key) {
            case 1:
                System.out.println("学生信息：");
                fileUtil.queryAll(studentFilePath);
                break;
            case 2:
                System.out.println("老师信息：");
                fileUtil.queryAll(teacherFilePath);
                break;
            case 3:
                System.out.println("书籍信息：");
                fileUtil.queryAll(bookFilePath);
                break;
            case 4:
                System.out.println("删除学生：");
                delete(studentFilePath, 1);
                break;
            case 5:
                System.out.println("删除老师：");
                delete(teacherFilePath, 2);
                break;

            case 6:
                System.out.println("删除书：");
                delete(bookFilePath, 3);
                break;
            case 7:
                System.out.println("增加一名学生：");
                studentService.add();
                break;

            case 8:
                System.out.println("修改学生姓名信息：");
                studentService.updateName();
                break;

            case 9:
                System.out.println("修改学生年龄信息：");
                studentService.updateAge();
                break;
            case 10:
                System.out.println("在指定位置添加一本书：");
                bookService.add();
                break;
            case 11:
                System.out.println("修改指定书籍名称：");
                bookService.updateName();
                break;

            case 12:
                System.out.println("在末尾添加一名老师：");
                teacherService.add();
                break;
            case 13:
                System.out.println("修改指定老师姓名：");
                teacherService.updateName();
                break;
            case 14:
                System.out.println("备份教师操作：");
                teacherService.beck();
                break;

        }
        System.out.println("-----------start--------------");
        System.out.println("输入0 继续操作");
        sc = new Scanner(System.in);
        key = sc.nextInt();
        if (key == 0) {
            start();
        }
    }

    private void initWord() {
        FileUtil fileUtil = new FileUtil();
        Utils.buildStuDtos(fileUtil.queryAll(studentFilePath));
        Utils.buildTeaDtos(fileUtil.queryAll(teacherFilePath));
        Utils.buildBookDtos(fileUtil.queryAll(bookFilePath));
    }


    private void delete(String path, Integer type) {
        if (type == 3) {
            BookService bookService = new BookService();
            bookService.deleteById(bookDtos.size() - 1);
            System.out.println("-------删除成功--------------");
            return;
        }
        FileUtil fileUtil = new FileUtil();
        fileUtil.queryAll(path);
        System.out.println("-------请输入"+ Utils.getNameByType(type)+"id----------");
        Scanner sc = new Scanner(System.in);
        int id = sc.nextInt();

        if (type == 1) {
            StudentService studentService = new StudentService();
            studentService.deleteById(id);
        }else if (type == 2) {
            TeacherService teacherService = new TeacherService();
            try {
                teacherService.deleteById(id);
            }catch (Exception e) {
                System.out.println(e.getMessage());
            }

        }

        System.out.println("-------删除成功--------------");

    }

    public void showPage() {
        System.out.println("💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗");
        System.out.println("******* 学生管理系统   *******");
        System.out.println("*******1.查询所有学生信息*******");
        System.out.println("*******2.查询所有老师信息*******");
        System.out.println("*******3.查询所有书籍信息*******");
        System.out.println("*******4.删除指定学生*******");
        System.out.println("*******5.删除指定老师*******");
        System.out.println("*******6.删除最后一本书*******");
        System.out.println("*******7.在末尾增加一名学生 *******");
        System.out.println("*******8.修改指定学生姓名    *******");
        System.out.println("*******9.修改指定学生年龄    *******");
        System.out.println("*******10.在指定位置添加一本书    *******");
        System.out.println("*******11.修改指定书籍名称    *******");
        System.out.println("*******12.在末尾添加一名老师   *******");
        System.out.println("*******13.修改指定老师姓名   *******");
        System.out.println("*******14.备份教师文件   *******");
        System.out.println("💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗💗");
    }

}
